//
//  UserDialectsViewController.m
//  DialectShare
//
//  Created by timmy zhong on 4/3/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "UserDialectsViewController.h"
#import "CachedImage.h"
#import "DialectDB.h"
#import "Dialect.h"
#import "DialectCell.h"
#import "DialectProxy.h"
#import "ListenViewController.h"

@interface UserDialectsViewController ()
@property (nonatomic, retain) NSArray *dialects;
@property (nonatomic, retain) DialectProxy *proxy;
@end

@implementation UserDialectsViewController
@synthesize name = _name, avatarContainer = _avatarContainer, moto = _moto, tableView = _tableView;
@synthesize user = _user, dialects = _dialects, proxy = _proxy;

- (id)init
{
    self = [super init];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}

- (void)dealloc
{
    self.name = nil;
    self.avatarContainer = nil;
    self.moto = nil;
    self.tableView = nil;
    self.user = nil;
    self.dialects = nil;
    self.proxy.delegate = nil;
    self.proxy = nil;
    
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.name.text = self.user.name;
    self.moto.text = self.user.moto;
    CachedImage *avatar = [[CachedImage alloc] initWithFrame:self.avatarContainer.bounds];
    [self.avatarContainer addSubview:avatar];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"user_icon" ofType:@"png"];
    [avatar showImageForUrl:self.user.avatar defaultImage:path];
    
    _proxy = [[DialectProxy alloc] init];
    _proxy.delegate = self;
    [_proxy dialectsOfUser:self.user];
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (IBAction)follow:(id)sender
{
    NSLog(@"fdsfds");
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dialects count];
}

- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return DialectCellHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"identifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[[DialectCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
    }
    Dialect *dialect = [_dialects objectAtIndex:indexPath.row];
    [((DialectCell *)cell) showDialect:dialect]; 
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Dialect *dialect = [self.dialects objectAtIndex:indexPath.row];
    ListenViewController *controller = [[ListenViewController alloc] init];
    controller.canShowUserDialects = NO;
    controller.dialect = dialect;
    [self.navigationController pushViewController:controller animated:YES];
    [controller release];
}

- (void)requestFinishWithProxy:(Proxy *)proxy
{
    DialectDB *db = [[DialectDB alloc] init];
    self.dialects = [db dialectsOfUser:self.user start:0 count:1000];
    [db release];
    
    [self.tableView reloadData];
}

- (void)requestFailWithProxy:(Proxy *)proxy
{
    
}

@end
